package com.aube.video.ad;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.aube.R;
import com.huyn.baseframework.utils.Utils;

public class PrerollVideoLayout extends FSVideoView implements MediaPlayer.OnPreparedListener, View.OnTouchListener {

    private static final String TAG = "FSVideoLayout";

    // Control views
    protected View videoControlsView;
    protected View.OnTouchListener touchListener;
    private int controlShowTime = 4000;

    public PrerollVideoLayout(Context context) {
        super(context);
    }

    public PrerollVideoLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PrerollVideoLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init() {
        Utils.Log(TAG, "init");

        super.init();

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.videoControlsView = inflater.inflate(R.layout.ad_videocontrols, null);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(ALIGN_PARENT_BOTTOM);

        // Start controls invisible. Make it visible when it is prepared
        this.videoControlsView.setVisibility(View.INVISIBLE);
    }

    @Override
    public void setOnTouchListener(View.OnTouchListener l) {
        touchListener = l;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        Utils.Log(TAG, "onCompletion");

        super.onCompletion(mp);
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        boolean result = super.onError(mp, what, extra);
        return result;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (getCurrentState() == State.END) {
            Utils.Log(TAG, "onDetachedFromWindow END");
        }
    }

    @Override
    protected void tryToPrepare() {
        Utils.Log(TAG, "tryToPrepare");
        super.tryToPrepare();

        if (getCurrentState() == State.PREPARED || getCurrentState() == State.STARTED) {
            videoControlsView.setVisibility(View.VISIBLE);
            mHandler.removeMessages(10);

            mHandler.sendEmptyMessageDelayed(10, controlShowTime);
        }
    }

    private TipViewClickedListener mListener = null;
    private ImageView mTipView = null;
    RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    public interface TipViewClickedListener {
        public void onTipViewClicked(View v);
    }

    public void setTipViewClickedListener(TipViewClickedListener listener) {
        mListener = listener;
    }

    public void configTipView(boolean isDownloadApp) {
        mParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        mParams.addRule(RelativeLayout.ABOVE, videoControlsView.getId());
        mParams.setMargins(0, 0, 10, 5);
        mTipView = new ImageView(context);
        if (isDownloadApp) {
            mTipView.setImageDrawable(getResources().getDrawable(R.drawable.tips_download));
        } else {
            mTipView.setImageDrawable(getResources().getDrawable(R.drawable.tips_detail));
        }
        mTipView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onTipViewClicked(v);
                }
            }
        });
        addView(mTipView, mParams);
    }

    @Override
    public void start() throws IllegalStateException {
        Utils.Log(TAG, "start");

        if (!isPlaying()) {
            super.start();
        }
    }

    @Override
    public void pause() throws IllegalStateException {
        Utils.Log(TAG, "pause");

        if (isPlaying()) {
            super.pause();
        }
    }

    @Override
    public void reset() {
        Utils.Log(TAG, "reset");

        super.reset();
    }

    @Override
    public void stop() throws IllegalStateException {
        Utils.Log(TAG, "stop");

        super.stop();
    }

    public void hideControls() {
        Utils.Log(TAG, "hideControls");
        if (videoControlsView != null) {
            videoControlsView.setVisibility(View.INVISIBLE);
        }
    }

    public void showControls() {
        Utils.Log(TAG, "showControls");
        if (videoControlsView != null) {
            videoControlsView.setVisibility(View.VISIBLE);
        }

        mHandler.removeMessages(10);
        mHandler.sendEmptyMessageDelayed(10, controlShowTime);
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 10) {
                // showOrHideController();
                hideControls();
            }
            return false;
        }
    });

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (videoControlsView != null) {
                if (videoControlsView.getVisibility() == View.VISIBLE) {
                    mHandler.removeMessages(10);
                    hideControls();
                } else {
                    showControls();
                }
            }
        }

        if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            return true;
        }

        if (touchListener != null) {
            return touchListener.onTouch(PrerollVideoLayout.this, event);
        }

        return false;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed == true && mTipView != null) {
            if (activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                mParams.width = getWidth() / 8;
                mParams.height = getHeight() / 5;
                mParams.setMargins(0, 0, 10, -50);
            } else {
                mParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
                mParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
                mParams.setMargins(0, 0, 10, 5);
            }
        }
    }

}
